package wibrst.visual.effect {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author wibrst@qq.com
	 */
	public class VEffectMouseFollow extends Sprite{
		
		public function VEffectMouseFollow( bAutoSet:Boolean = true ) {
			_bAutoSet = bAutoSet;
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, removedHandler);
			// entry point
			
			//trace( this, "mouseHandler!!!!!!!!!!!" );
			this.mouseEnabled = false;
			this.mouseChildren = false;
			
			if ( _bAutoSet) active( true );
		}
		
		public function active( bSet:Boolean ):Boolean {
			var bSuc:Boolean = false;
			if ( stage ) {
				if (bSet) {
					stage.addEventListener( MouseEvent.MOUSE_MOVE, mouseHandler );
				}else {
					stage.removeEventListener( MouseEvent.MOUSE_MOVE, mouseHandler );
				}
				bSuc = true;
			}
			return bSuc;
		}
		
		private function removedHandler(e:Event):void {
			removeEventListener(Event.REMOVED_FROM_STAGE, removedHandler);
			if (stage) stage.removeEventListener( MouseEvent.MOUSE_MOVE, mouseHandler );
		}
		
		private function mouseHandler( evt:MouseEvent ):void {
			var particle:Particle = new Particle( this );
			particle.x = mouseX;
			particle.y = mouseY;
			particle.addEventListener( Particle.OVER, particleOverHandler );
			this.addChild( particle );
			
			//trace( this, "mouseHandler:", particle );
		}
		
		private function particleOverHandler( evt:Event ):void {
			var particle:Particle = evt.currentTarget as Particle;
			this.removeChild( particle );
		}
		
		private var _bAutoSet:Boolean;
	}

}

import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Shape;
import flash.events.Event;
import wibrst.layout.GlbLayout;
import wibrst.utils.FilterUtil;

class Particle extends Shape {
	
	static public const OVER:String = "OVER";
	
	public function Particle( vHost:DisplayObject ) {
		_vHost = vHost;
		
		this.graphics.beginFill( 0xffffff, 1 );
		this.graphics.drawCircle( 0, 0, 3 + Math.random() * 3 );
		this.graphics.endFill();
		this.filters = [ FilterUtil.getBlurFilter() ];
		
		var radian:Number = Math.PI * 2 * Math.random();
		var speed:Number = 2 + 3 * Math.random();
		_xStep = speed * Math.cos( radian );
		_yStep = speed * Math.sin( radian );
		
		this.addEventListener( Event.ENTER_FRAME, engineHandler );
	}
	
	private function engineHandler( evt:Event ):void {
		move();
		
		if ( this.alpha < .01 
			|| this.x < 0 || this.x > GlbLayout.STG_W || this.y < 0 || this.y > GlbLayout.STG_H ) { 
			this.halt();
			this.dispatchEvent( new Event( OVER ) );
			this._vHost = null;
		}
	}
	
	private function move():void{
		//this.x += _xStep;
		//this.y += _yStep;
		
		//_xDist = _vHost.mouseX - this.x //+ _xDist*.8;
		//_yDist = _vHost.mouseY - this.y //+ _yDist*.8;
		
		//this.x += _xDist * .1;
		//this.y += _yDist * .1;
		
		//this.y += 2;
		
		this.alpha -= .03;
		this.scaleX -= .01;
		this.scaleY -= .01;
	}
	
	private function halt():void{
		this.removeEventListener( Event.ENTER_FRAME, engineHandler );
	}
	
	private var _xStep:Number;
	private var _yStep:Number;
	
	private var _xDist:Number;
	private var _yDist:Number;
	
	/// dump
	private var _vHost:DisplayObject;
	
	//private var _radian:Number;
	//private var _speed:Number;
	
}

